package com.aaa.controller;

import com.aaa.entity.User;
import com.aaa.service.impl.UserServiceImpl;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("user")
@RestController
public class Usercontroller {
    @Resource
    UserServiceImpl userService;

    @RequestMapping("finduser")
    public Map<String,Object> finduser(Integer uid){
        return userService.finduser(uid);
    }

    @RequestMapping("updateuser")
    public Integer updateuser(User user){
        return userService.updateuser(user);
    }

    @RequestMapping("updpass")
    public Integer updpass(String password,Integer id){
        BCryptPasswordEncoder bCryptPasswordEncoder=new BCryptPasswordEncoder();
        String s = bCryptPasswordEncoder.encode(password);
        return userService.updpass(s,id);
    }


    @RequestMapping("find")
    public Map<String, Object> find(Integer pageSize,Integer pageNum){
        Map<String, Object> map = new HashMap<>();
        List<User> userList = userService.find(pageSize, pageSize*pageNum );
        Integer TotalNum = userService.findcount();

        map.put("userList", userList);
        map.put("TotalNum", TotalNum);
        return map;
    }

    @RequestMapping("add")
    @ResponseBody
    public int add(User user){
        user.setEntry(new Date());
        return userService.add(user);
    }

    @ResponseBody
    @RequestMapping("delete")
    public int delete(Integer uid){
        System.out.println(uid);
        return userService.delete(uid);
    }
    @RequestMapping("update")
    public int update(User user){
        return userService.update(user);
    }

    @RequestMapping("finduser1")
    public List<User> finduser(){
        return userService.finduser();
    }
    @RequestMapping("findauditor")
    public List<User> findauditor(){
        return userService.findauditor();
    }
}
